//
//  MLOAuthViewController.swift
//  MLWeiBo
//
//  Created by wenxing on 2017/1/7.
//  Copyright © 2017年 Se7venX. All rights reserved.
//

import UIKit
import SVProgressHUD

/**
 
 1.授权码 token
 2.令牌 access_token
 3.获取用户信息
 
 **/

class MLOAuthViewController: UIViewController,UIWebViewDelegate {
    
    let webview = UIWebView()
    
    
    override func loadView() {
        
        view = webview
        
        webview.delegate = self
        
        //请求
        loadNetData()
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", tatget: self, selector: #selector(clickClose))
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "走后门", tatget: self, selector: #selector(clickZouHouMen))
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        SVProgressHUD.dismiss()
        
    }
    
    //MARK:- 加载web页面 -
    private func loadNetData() {
        
        let urlString = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + AppKey + "&redirect_uri=" + App_redirect_uri
        
//        print("urlString",urlString)
        
        let url = URL(string: urlString)
        
        let req = URLRequest(url: url!)
        
        webview.loadRequest(req)
    }

    //MARK:- 点击关闭 -
    @objc private func clickClose() {
    
        dismiss(animated: true, completion: nil)
    }
    
    //MARK:- 走后门 -
    @objc private func clickZouHouMen() {
        
        
        let jsString = "document.getElementById('userId').value = \"562668485@qq.com\",document.getElementById('passwd').value = \"ios1370403840\""
        
       webview.stringByEvaluatingJavaScript(from: jsString)
    }
    
    //MARK:- WebviewDelegate -
    /*
     * 第一步：请求了token 授权码
     */
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    
        let url = request.url?.absoluteString ?? ""
        
        if  url.contains("code=") {
            
            let query = request.url?.query ?? ""
            let code = (query as NSString).substring(from: "code=".characters.count)
            
            // code=3ab7a6a46cdc6343e388e801c8246251
            // print("此处注意:::",code)
            
//            self.loadAccess_Token(code: code)
            
            MLUserAccountViewModel.shared.loadAccess_Token(code: code,finished: { (isSuc) in
            
                print("isSuc",isSuc)
                
                
                UIApplication.shared.keyWindow?.rootViewController = MLWelcomeViewController()
                
            })
            
            return false
        }
        
        return true
    }
    
    func webViewDidStartLoad(_ webView: UIWebView) {
        
        SVProgressHUD.show()
    }
    
    func webViewDidFinishLoad(_ webView: UIWebView) {
        
        SVProgressHUD.dismiss()
    }
    
    func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
        SVProgressHUD.dismiss()
    }
}
